<?php
namespace Model\Saver;

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;

/**
 * Description of AbstractSaver
 *
 * @author pahhan
 */
abstract class AbstractSaver implements SaverInterface, LoggerAwareInterface
{
    protected $options = array();

    /**
     *
     * @var LoggerInterface
     */
    protected $logger;

    protected $has_logger = FALSE;

    public function __construct(array $options = array())
    {
        $this->options = $options;
    }

    public function save(array $messages)
    {
        $i = 0;
        foreach ( $messages as $message ){
            $this->saveOne($message);
            ++$i;
        }
        if( $this->has_logger ) {
            $this->logger->info(sprintf('%d message(s) saved', $i));
        }
        $this->afterSave();
    }

    abstract protected function saveOne(\Model\Entity\Message $message);

    protected function afterSave(){}

    public function setOptions(array $options)
    {
        $this->options = $options;
    }

    public function hasOption($name)
    {
        return array_key_exists($name, $this->options);
    }

    public function setLogger(LoggerInterface $logger) {
        $this->logger = $logger;
        $this->has_logger = TRUE;
    }

}
